#include<bits/stdc++.h>
using namespace std;
#define ll long long int
long long int mod= 1e9+7;
int main(){
ll n,k,a,b;
cin>>n>>k>>a>>b;
ll ans=0;
if(n<k){
cout<<a*n-a;
return 0;
}
else if(n==k){
cout<<min(n*a-a,b);
return 0;
}else if(k==1){
cout<<(n-1)*a;
return 0;
}
while(n>1){
if(n%k==0){
ll tem=n/k;
ll cost=0;
if(tem==1)cost=(k-1)*a;
else cost=(n-tem)*a;
ans+=min(b,cost);
n/=k;
}else{
ll x=n/k;
ll tem=x*k;
if(tem==0)tem++;
ans+=(n-tem)*a;
n=tem;
}
// cout<<ans<<" ";
}
cout<<ans;
return 0;
}
13 Reasons Why | Friend's Relationship |
Health of a person | Divisibility |
A. Movement | Numbers in a matrix |
Sequences | Split houses |
Divisible | Three primes |
Coprimes | Cost of balloons |
One String No Trouble | Help Jarvis! |
Lift queries | Goki and his breakup |
Ali and Helping innocent people | Book of Potion making |
Duration | Birthday Party |
e-maze-in | Bricks Game |
Char Sum | Two Strings |
Anagrams | Prime Number |
Lexical Sorting Reloaded | 1514A - Perfectly Imperfect Array |
580A- Kefa and First Steps | 1472B- Fair Division |